<template>
  <!-- 用户平台订单列表 -->
  <ContentWrap>
    <el-form
      ref="queryFormRef"
      :inline="true"
      :model="queryParams"
      class="-mb-15px"
      label-width="68px"
    >
      <!--      <el-form-item label="创建时间" prop="createTime">-->
      <!--        <el-date-picker-->
      <!--          v-model="queryParams.createTime"-->
      <!--          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"-->
      <!--          class="!w-280px"-->
      <!--          end-placeholder="自定义时间"-->
      <!--          start-placeholder="自定义时间"-->
      <!--          type="daterange"-->
      <!--          value-format="YYYY-MM-DD HH:mm:ss"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="原始单号" prop="srcTradeNo">
        <el-input
          v-model="queryParams.srcTradeNo"
          placeholder="请输入原始单号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="平台来源" prop="platformId">
        <el-select v-model="queryParams.platformId" class="!w-280px" clearable placeholder="全部">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.PLATFORM_ID_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button @click="handleQuery">
          <Icon class="mr-5px" icon="ep:search" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon class="mr-5px" icon="ep:refresh" />
          重置
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
  <ContentWrap>
    <el-table v-loading="loading" :data="list">
      <el-table-column
        key="srcTradeNo"
        align="center"
        label="原始订单号"
        width="180"
        prop="srcTradeNo"
      >
        <template #default="scope">
          <el-link @click="getDetail(scope.row)" type="primary">
            {{ scope.row.srcTradeNo }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column align="center" label="来源平台" min-width="100">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.PLATFORM_ID_TYPE" :value="scope.row.platformId" />
        </template>
      </el-table-column>
      <el-table-column label="旺店通出库单号" min-width="150" prop="orderNo" />
      <el-table-column label="订单金额" min-width="100" prop="receivable" />
      <el-table-column label="店铺名称" min-width="100" prop="shopName" />
      <el-table-column align="center" label="支付时间" prop="payTime" width="180px">
        <template #default="scope">
          {{ dayjs(Number(scope.row.payTime)).format('YYYY-MM-DD HH:mm:ss') }}
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
  <Dialog title="订单详情" top="30vh" width="1200px" v-model="dialogVisible">
    <el-descriptions title="订单详情" :column="3">
      <el-descriptions-item label="原始单号：">
        {{ detailInfo.srcTradeNo }}
      </el-descriptions-item>
      <el-descriptions-item label="来源平台：">
        <dict-tag :type="DICT_TYPE.PLATFORM_ID_TYPE" :value="detailInfo.platformId" />
      </el-descriptions-item>
      <el-descriptions-item label="旺店通出库单号：">
        {{ detailInfo.orderNo }}
      </el-descriptions-item>
      <el-descriptions-item label="店铺名称：">
        {{ detailInfo.shopName }}
      </el-descriptions-item>
      <el-descriptions-item label="订单金额（应付）：">
        {{ detailInfo.receivable }}
      </el-descriptions-item>
      <el-descriptions-item label="货品总额：">
        {{ detailInfo.goodsTotalAmount }}
      </el-descriptions-item>
      <el-descriptions-item label="物流单号：">
        {{ detailInfo.logisticsNo }}
      </el-descriptions-item>
    </el-descriptions>
    <div>商品明细：</div>
    <el-table v-loading="loading" :data="detailInfo.detailList">
      <el-table-column label="序号" min-width="80" type="index" />
      <el-table-column align="center" label="商品名称" width="180" prop="goodsName" />
      <el-table-column align="center" label="sku" width="250" prop="specName" />
      <el-table-column label="物料编码" min-width="150" prop="specNo" />
      <el-table-column label="单价" min-width="100" prop="marketPrice" />
      <el-table-column label="数量" min-width="100" prop="goodsCount" />
    </el-table>
  </Dialog>
</template>

<script lang="ts" setup>
defineOptions({ name: 'TradePlatformOrder' })

import { DICT_TYPE, getIntDictOptions, getStrDictOptions } from '@/utils/dict'
import * as OrderApi from '@/api/mall/trade/order/index'
import dayjs from 'dayjs'

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: undefined,
  createTime: [],
  platformId: undefined,
  srcTradeNo: undefined
})
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await OrderApi.getPlatformOrderPageAll(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}
const dialogVisible = ref(false)
const detailInfo = ref({})
/** 查询列表 */
const getDetail = async (item: any) => {
  loading.value = true
  try {
    const data = await OrderApi.getPlatformOrder(item.stockoutId)
    detailInfo.value = {
      detailList: data,
      ...item
    }
    dialogVisible.value = true
    console.log('detailInfo', detailInfo)
  } finally {
    loading.value = false
  }
}
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
